package com.sholmes.android.scribbler;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public abstract class BasicRobotActivity extends Activity {
	private static final String TAG = "------>BasicRobotActivity: ";

	protected ITestService service;
	
	protected Intent intent; // was originally private
	
	protected TestServiceConnection connection;
	
	@Override
	protected void onStart(){		
		Log.d(TAG,"onStart");
		initService() ;
		super.onStart();
	}
	@Override
	protected void onStop(){		
		Log.d(TAG,"onStop");
		releaseService();
		super.onStop();
	}
	
	
	protected BasicRobotActivity(){
		  intent = new Intent();
		  intent.setClassName("com.sholmes.android.scribbler", com.sholmes.android.scribbler.ScribblerService.class.getName());
	}



	
	  /**
	   * This class represents the actual service connection. It casts the bound
	   * stub implementation of the service to the AIDL interface.
	   */
	  class TestServiceConnection implements ServiceConnection {
		@Override
		public void onServiceConnected(ComponentName name, IBinder boundService) {
			 service = ITestService.Stub.asInterface((IBinder) boundService);
		      Log.d(TAG, "ITestService onServiceConnected() connected");			
		      
		      
		    	  try {
					service.registerCallback(callback);
				} catch (RemoteException e) {
					Log.e(TAG,"Error calling Scribbler service registerCallback.  Check stack trace for details.",e);
				}
		      
		      
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			service = null;
		    Log.d(TAG, "ITestService onServiceDisconnected() disconnected");		
		} 
	  }
	  
	  /** Binds this activity to the service. */
	  protected void initService() {
		  connection = new TestServiceConnection();
	    
	    
	    
	    boolean ret = bindService(intent, connection, Context.BIND_AUTO_CREATE);
	    startService(intent);
	    Log.d(TAG, "initService() bound with " + ret);

	  }

	  /** Unbinds this activity from the service. */
	  protected void releaseService() {
			try {				
				service.unregisterCallback(callback);
			} catch (RemoteException e) {
				Log.e(TAG,"Error calling Scribbler service unregisterCallback.  Check stack trace for details.",e);				
			}
		  
		unbindService(connection);	    
	    connection = null;
	    
	    Log.d(TAG, "releaseService() unbound.");
	  }
	  
	  
	  protected void killService(){
		  Log.d(TAG,"Stopping service: " + stopService(intent));
	  }
	  
	  
	  private ITestService callback = new ITestService.Stub() {
			
			@Override
			public void unregisterCallback(ITestService cb) throws RemoteException {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void registerCallback(ITestService cb) throws RemoteException {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public int getState() throws RemoteException {
				// TODO Auto-generated method stub
				return 0;
			}
			
			@Override
			public void disconnected(String message) throws RemoteException {
				Log.d(TAG,"Return from disconnect");						
				mHandler.sendMessage(mHandler.obtainMessage(ScribblerService.STATE_DISCONNECTED, message));
			}

			
			@Override
			public void connecting(String device) throws RemoteException {
				Log.d(TAG,"Return from connecting");			
				mHandler.sendMessage(mHandler.obtainMessage(ScribblerService.STATE_CONNECTING, device));
			}
			
			@Override
			public void connected(String device) throws RemoteException {
				Log.d(TAG,"Return from connected");
				mHandler.sendMessage(mHandler.obtainMessage(ScribblerService.STATE_CONNECTED, device));			
			}
			
			@Override
			public void connect(String deviceAddress) throws RemoteException {
				// TODO Auto-generated method stub
				
			}
			
			
			@Override
			public void disconnect() throws RemoteException {
				
			}

			@Override
			public void drive(int speed, int direction) throws RemoteException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void stop() throws RemoteException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public int[] getSensors() throws RemoteException {
				// TODO Auto-generated method stub
				return null;
			}

		};
		  
		protected Handler mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what){
				case ScribblerService.STATE_DISCONNECTED:
					Toast.makeText(getApplicationContext(), (String)msg.obj, Toast.LENGTH_LONG).show();			
					_disconnect();
					break;
				case ScribblerService.STATE_CONNECTED:
					_connected((String)msg.obj);
					break;
				case ScribblerService.STATE_CONNECTING:
					Toast.makeText(getApplicationContext(), "Connecting to device: " +(String)msg.obj , Toast.LENGTH_LONG).show();
					_connecting((String)msg.obj);
					break;
				}
			}		
		};
		
		abstract void _disconnect();
		abstract void _connected(String device);
		abstract void _connecting(String device);
}
